﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MyLibary.DAL;
using System.Data.SqlClient;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

namespace Test
{
    public partial class Form1 : Form
    {
        public SqlConnection connet = new SqlConnection(@"Data Source=.\SQLEXPRESS;Database=DongHo;Integrated Security=true");
        public Form1()
        {
            InitializeComponent();
        }

        private void Save(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "JPG Files(*.JPG)|*.JPG|GIF Files(*.GIF)|*.GIF";
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                Store(dlg.FileName);
                }
            
        }
        private void Store(string filename)
        {
            byte[] imageData = null;
            // Read the file into a byte array
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                imageData = new Byte[fs.Length];
                fs.Read(imageData, 0, (int)fs.Length);
            }
            SqlCommand cmd = new SqlCommand("InsertPro", connet);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MaSanPham","123");
            cmd.Parameters["@MaSanPham"].Direction = ParameterDirection.Input;
            cmd.Parameters.AddWithValue("@TenSanPham", "123");
            cmd.Parameters["@TenSanPham"].Direction = ParameterDirection.Input;
            cmd.Parameters.AddWithValue("@GiaNhap",123);
            cmd.Parameters["@GiaNhap"].Direction = ParameterDirection.Input;
            cmd.Parameters.AddWithValue("@GiaBan", 123);
            cmd.Parameters["@GiaBan"].Direction = ParameterDirection.Input;
            cmd.Parameters.AddWithValue("@MoTa","1");
            cmd.Parameters["@MoTa"].Direction = ParameterDirection.Input;
            cmd.Parameters.Add("@Anh", SqlDbType.Image);
            cmd.Parameters["@Anh"].Direction = ParameterDirection.Input;
            cmd.Parameters["@Anh"].Value = imageData;
            cmd.Parameters.AddWithValue("@MaLoai","0001");
            cmd.Parameters["@MaLoai"].Direction = ParameterDirection.Input;
            connet.Open();
            cmd.ExecuteNonQuery();
            connet.Close();
            
        }
        public Image resizeImage(Image img, int width, int height)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage((Image)b);

            g.DrawImage(img, 0, 0, width, height);
            g.Dispose();

            return (Image)b;
        }

        private void Load(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("SELECT Anh FROM SanPham WHERE MaSanPham='123'", connet);
            if(connet.State!=ConnectionState.Open)
                connet.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
            reader.Read();
            long size = reader.GetBytes(0, 0, null, 0, 0);
            byte[] imageData = new byte[size];
            long bytesread = 0;
            int curpos = 0;
            int chunkSize = 1;
            while (bytesread < size)
            {
                // chunkSize is an arbitrary application defined value
                bytesread += reader.GetBytes(0, curpos, imageData, curpos, chunkSize);
                curpos += chunkSize;
            }
            MemoryStream ms = new MemoryStream(imageData);
            Image a = Image.FromStream(ms);
            
            reader.Close();
            connet.Close();
        }
        private static Image ConvertToImage(Object O)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms,O);
            return Image.FromStream(ms);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
